package com.qf.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qf.openapi2204.common.constants.RedisConstants;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@Component
public class ErrFilter extends ZuulFilter {



    @Override
    public String filterType() {
        return FilterConstants.ERROR_TYPE;
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {

        RequestContext currentContext = RequestContext.getCurrentContext();
        boolean result = currentContext.sendZuulResponse();

        return result;
    }

    @Override
    public Object run() throws ZuulException {

        RequestContext ctx = RequestContext.getCurrentContext();
        Throwable throwable = ctx.getThrowable();
        if (throwable instanceof ZuulRuntimeException) {
            ZuulRuntimeException zuulException = (ZuulRuntimeException) throwable;
            // 设置自定义的错误信息
            ctx.setResponseBody("Custom Error Message");
            ctx.getResponse().setContentType("application/json");
            ctx.setResponseStatusCode(500);
        }
        return null;


    }
}
